﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;

namespace socket_ping工具
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        string host;
        int port;

        Thread t;
        List<String> LIST1 = new List<string>();

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            host = textBox3.Text;
        }

        private void pingTask(object i)
        {
            IPAddress ip = IPAddress.Parse(host + i.ToString());

            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //设定服务器IP地址  
            if (radioButton1.Checked == true)
            {
                try
                {
                    clientSocket.Connect(new IPEndPoint(ip, 22)); //配置服务器IP与端口  
                    LIST1.Add(host + i.ToString());
                }
                catch
                {
                    return;
                }
            }
            else if (radioButton2.Checked == true)
            {
                try
                {
                    clientSocket.Connect(new IPEndPoint(ip, 5938)); //配置服务器IP与端口  
                    LIST1.Add(host + i.ToString());
                }
                catch
                {
                    return;
                }
            }
            else
            {
                try
                {
                    clientSocket.Connect(new IPEndPoint(ip, port)); //配置服务器IP与端口  
                    LIST1.Add(host + i.ToString());
                }
                catch
                {
                    return;
                }
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            t.Abort();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LIST1.Clear();
            textBox1.Clear();
            for (int i = 0; i < 255; i++)
            {
                t = new Thread(pingTask);
                t.Start(i);
            }
            foreach (var s in LIST1)
            {
                textBox1.AppendText(s + "\r\n");
            }
            MessageBox.Show("OK");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            LIST1.Clear();
            textBox1.Clear();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            host = textBox3.Text;
            port = int.Parse(textBox5.Text);
            //t = new Thread(pingTask);
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            try
            {
                port = int.Parse(textBox5.Text);
            }
            catch
            { }
        }
    }
}
